package com.one.controller;

import com.one.pojo.OneSuper;
import com.one.pojo.Store;
import com.one.service.OneSuperService;
import com.one.service.StoreService;
import com.one.util.EhcacheCacheProvider;
import com.one.util.JSONResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 超级孵化器
 * @Author semstouch
 * @Date 2017/4/8
 **/
@Controller
@ControllerAdvice
public class OneSuperController extends AbstractJsonpResponseBodyAdvice {
    @Resource
    private StoreService storeService;
    @Resource
    private OneSuperService oneSuperService;

    @Resource(name="ehcacheCacheProvider")
    private EhcacheCacheProvider cacheProvider;


    public  OneSuperController(){
        super("callback");
    }


    @RequestMapping(value = "/buySuper", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult buySuper(HttpServletRequest request,String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        Store store = storeService.findByUserId(userId);
        BigDecimal chicksNum = store.getChicksNum();
        if(chicksNum.intValue()<1000){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户小鸡数量不够|Your chicken is not enough");
            return  jsonResult;
        }
        chicksNum= chicksNum.subtract(new BigDecimal(1000));
        store.setChicksNum(chicksNum);
        OneSuper oneSuper = new OneSuper();
        oneSuper.setUserId(userId);
        oneSuper=oneSuperService.add(oneSuper,store);
        jsonResult.setCode("1");
        jsonResult.setData(oneSuper);
        return  jsonResult;
    }

    @RequestMapping(value = "/putEggToSuper", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult putEggToSuper(String token,String  userId,int eggNum) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        if(eggNum<0){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户鸡蛋数量有误|Your eggNum is error");
            return  jsonResult;
        }
        Store store = storeService.findByUserId(userId);
        BigDecimal storeEggNum = store.getEggsNum();
        if(storeEggNum.intValue()<Integer.valueOf(eggNum)){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户鸡蛋数量不够|Your egg is not enough");
            return  jsonResult;
        }

        OneSuper oneSuper = oneSuperService.find(userId);
        BigDecimal newEggNum=oneSuper.getEggNum().add(new BigDecimal(eggNum));
        oneSuper.setEggNum(newEggNum);
        store.setEggsNum(storeEggNum.subtract(new BigDecimal(eggNum)));
        oneSuperService.update(oneSuper,store);

        Map rs = new HashMap();
        rs.put("superIncubator",oneSuper);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    @RequestMapping(value = "/getEggFromSuper", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getEggFromSuper(String token,String  userId,int eggNum) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }
        if(eggNum<0){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户鸡蛋数量有误|Your eggNum is error");
            return  jsonResult;
        }
        Store store = storeService.findByUserId(userId);
        OneSuper oneSuper = oneSuperService.find(userId);

        BigDecimal superEggNum = oneSuper.getEggNum();
        if(superEggNum.intValue()<Integer.valueOf(eggNum)){
            jsonResult.setCode("-1");
            jsonResult.setMsg("账户鸡蛋数量不够|Your egg is not enough");
            return  jsonResult;
        }


        oneSuper.setEggNum(oneSuper.getEggNum().subtract(new BigDecimal(eggNum)));
        store.setEggsNum(store.getEggsNum().add(new BigDecimal(eggNum)));
        oneSuperService.update(oneSuper,store);

        Map rs = new HashMap();
        rs.put("superIncubator",oneSuper);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

    @RequestMapping(value = "/getChickenFormSuper", method = RequestMethod.GET)
    @ResponseBody
    public JSONResult getChickenFormSuper(String token,String  userId) {
        JSONResult jsonResult = new JSONResult();
        if(!cacheProvider.get(userId).equals(token)){
            jsonResult.setCode("-2");
            jsonResult.setMsg("token失效|Invalid token");
            return jsonResult;
        }

        Store store = storeService.findByUserId(userId);
        OneSuper oneSuper = oneSuperService.find(userId);

        Integer chicksIncome=oneSuper.getChicksIncome();

        store.setChicksNum(store.getChicksNum().add(new BigDecimal(oneSuper.getChicksIncome())));
        oneSuper.setChicksIncome(0);
        oneSuper.setCollectFlag("0");
        oneSuperService.update(oneSuper,store);
        oneSuper.setChicksIncome(chicksIncome);

        Map rs = new HashMap();
        rs.put("superIncubator",oneSuper);
        jsonResult.setCode("1");
        jsonResult.setData(rs);
        return  jsonResult;
    }

}
